#########################################################################
#To make this figure, you must first run "make_full_panel.R" first.######
#########################################################################
rm(list = ls())
setwd("~/Desktop/QJPS Replication")
data <- read.csv("all_countries_ideal_points_panel.csv")

##################################################

#Calculate the relative similarity to US over time
data_us <- data[which(data$names2 == "US"),]
m <- match(data$year, data_us$year)
table(is.na(m))
data$us.score <- data_us$cscore[m]
data$dist_us2 <- abs(data$cscore - data$us.score)

#run a model with year dummies (and no intercept to see relative similarity over time, only since 1900 when we have 26 countries)
time_mod <- lm(dist_us2 ~ as.factor(year) - 1, data = data[data$cow != 2 & data$year >= 1900,])
se <- sqrt(diag(vcov(time_mod)))
upper <- coef(time_mod) + 1.96*se
lower <- coef(time_mod) - 1.96*se

plot(1900:2010, coef(time_mod), pch = 16, axes=F, xlab="", ylab="Distance", ylim = c(.4, 1.5), cex = .5, main = "Average Distance From the U.S. Constitution")
segments(x0 = 1900:2010, x1 = 1900:2010, y0 = lower, y1 = upper)
axis(1, at = seq(1900, 2010, 10))
axis(2, at = seq(0, 1.5, .25), las = 2)

##################################################

#Calculate the relative similarity to FR over time
data_fr <- data[which(data$names2 == "FR"),]

m <- match(data$year, data_fr$year)
table(is.na(m))
data$fr.score <- data_fr$cscore[m]

data$dist_fr2 <- abs(data$cscore - data$fr.score)

#run a model with year dummies (and no intercept to see relative similarity over time, only since 1900 when we have 26 countries)
time_mod_fr <- lm(dist_fr2 ~ as.factor(year) - 1, data = data[data$cow != 200 & data$year >= 1900,])
se_fr <- sqrt(diag(vcov(time_mod_fr)))
upper_fr <- coef(time_mod_fr) + 1.96*se_fr
lower_fr <- coef(time_mod_fr) - 1.96*se_fr

plot(1900:2010, coef(time_mod_fr), pch = 16, axes=F, xlab="", ylab="Distance", ylim = c(.4, 3.01), cex = .5, main = "France")
segments(x0 = 1900:2010, x1 = 1900:2010, y0 = lower_fr, y1 = upper_fr)
axis(1, at = seq(1900, 2010, 10))
axis(2, at = seq(0, 3, .5), las = 2)
#box()

##################################################

#Calculate the relative similarity to Germany over time
data_de <- data[which(data$names2 == "DE"),]

m <- match(data$year, data_de$year)
table(is.na(m))
data$de.score <- data_de$cscore[m]

data$dist_de <- abs(data$cscore - data$de.score)

#run a model with year dummies (and no intercept to see relative similarity over time, only since 1900 when we have 26 countries)
time_mod_de <- lm(dist_de ~ as.factor(year) - 1, data = data[data$cow != 255 & data$year >= 1900, ])
se_de <- sqrt(diag(vcov(time_mod_de)))
upper_de <- coef(time_mod_de) + 1.96*se_de
lower_de <- coef(time_mod_de) - 1.96*se_de

plot(1900:2010, coef(time_mod_de), pch = 16, axes=F, xlab="", ylab="Distance", ylim = c(.4, 3.01), cex = .5, main = "Germany", xlim = c(1900, 2010))
segments(x0 = 1900:2010, x1 = 1900:2010, y0 = lower_de, y1 = upper_de)
axis(1, at = seq(1900, 2010, 10))
axis(2, at = seq(0, 3, .5), las = 2)

##################################################

#Calculate the relative similarity to Canada over time
data_ca <- data[which(data$names2 == "CA"),]

m <- match(data$year, data_ca$year)
table(is.na(m))
data$ca.score <- data_ca$cscore[m]

data$dist_ca <- abs(data$cscore - data$ca.score)

#run a model with year dummies (and no intercept to see relative similarity over time, only since 1900 when we have 26 countries)
time_mod_ca <- lm(dist_ca ~ as.factor(year) - 1, data = data[data$cow != 20 & data$year >= 1900, ])
se_ca <- sqrt(diag(vcov(time_mod_ca)))
upper_ca <- coef(time_mod_ca) + 1.96*se_ca
lower_ca <- coef(time_mod_ca) - 1.96*se_ca

plot(1900:2010, coef(time_mod_ca), pch = 16, axes=F, xlab="", ylab="Distance", ylim = c(.4, 3.01), cex = .5, main = "Canada", xlim = c(1900, 2010))
segments(x0 = 1900:2010, x1 = 1900:2010, y0 = lower_ca, y1 = upper_ca)
axis(1, at = seq(1900, 2010, 10))
axis(2, at = seq(0, 3, .5), las = 2)

##################################################

#Calculate the relative similarity to India over time
data_in <- data[which(data$names2 == "IN"),]

m <- match(data$year, data_in$year)
table(is.na(m))
data$in.score <- data_in$cscore[m]

data$dist_in <- abs(data$cscore - data$in.score)

#run a model with year dummies (and no intercept to see relative similarity over time, only since 1900 when we have 26 countries)
time_mod_in <- lm(dist_in ~ as.factor(year) - 1, data = data[data$cow != 750 & data$year >= 1951, ])
se_in <- sqrt(diag(vcov(time_mod_in)))
upper_in <- coef(time_mod_in) + 1.96*se_in
lower_in <- coef(time_mod_in) - 1.96*se_in

plot(1951:2010, coef(time_mod_in), pch = 16, axes=F, xlab="", ylab="Distance", ylim = c(.4, 3.01), cex = .5, main = "India", xlim = c(1900, 2010))
segments(x0 = 1951:2010, x1 = 1951:2010, y0 = lower_in, y1 = upper_in)
axis(1, at = seq(1900, 2010, 10))
axis(2, at = seq(0, 3, .5), las = 2)
